package com.ziyueyuwen.xiniao.controller.user;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.vo.ZyUserVO;
import com.ziyueyuwen.xiniao.service.ThirdPartService;
import com.ziyueyuwen.xiniao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author wancheng  on 2018/12/18.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    ThirdPartService thirdPartService;

    @RequestMapping(value = "/get_user_message.do", method = RequestMethod.POST)
    public void getUserMessage(HttpServletRequest request,
                               HttpServletResponse response) {
        HttpResContent<ZyUserVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取用户详情");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            ZyUserVO user = userService.getUserMessage(userAccount);
            saveSuccessMsgAndData(hrc, user);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_students.do", method = RequestMethod.POST)
    public void getAllStudent(HttpServletRequest request,
                              HttpServletResponse response) {
        HttpResContent<List<ZyUser>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取所有学生");
        try {
            List<ZyUser> students = userService.getStudents();
            saveSuccessMsgAndData(hrc, students);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_students_by_name.do", method = RequestMethod.POST)
    public void getStudentsByAccountOrName(HttpServletRequest request,
                                           HttpServletResponse response,
                                           String userName) {
        HttpResContent<List<ZyUser>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取所有学生");
        try {
            List<ZyUser> students = userService.getStudentByAccountOrName(userName);
            saveSuccessMsgAndData(hrc, students);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_teachers.do", method = RequestMethod.POST)
    public void getTeacher(HttpServletRequest request,
                           HttpServletResponse response) {
        HttpResContent<List<ZyUser>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取所有老师");
        try {
            List<ZyUser> teachers = userService.getTeachers();
            saveSuccessMsgAndData(hrc, teachers);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_sys_user.do", method = RequestMethod.POST)
    public void getTeacher(HttpServletRequest request,
                           HttpServletResponse response,
                           Integer page,
                           Integer rows,
                           String query) {
        HttpResContent<PageData<ZyUserVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取系统用户");
        try {
            PageData<ZyUserVO> users = userService.getSysUser(query, page, rows);
            saveSuccessMsgAndData(hrc, users);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/sync_ding_talk.do", method = RequestMethod.POST)
    public void syncDingTalk(HttpServletRequest request,
                             HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("同步钉钉");
        try {
            thirdPartService.loadDingTalkUser();
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_user_nick_name.do", method = RequestMethod.POST)
    public void updateUserNickName(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam(PARAM_USER_ACCOUNT) String userAccount,
                                   @RequestParam(PARAM_NICK_NAME) String nickName) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改昵称");
        try {
            ZyUser user = userService.getByAccount(userAccount);
            ZyUser updateUser = new ZyUser();
            updateUser.setId(user.getId());
            updateUser.setNickName(nickName);
            userService.updateStudentName(updateUser);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
